package com.soo.hoo.admin.api;

import cn.hutool.core.bean.BeanUtil;
import com.soo.hoo.admin.model.dto.FileDTO;
import com.soo.hoo.admin.model.dto.ImageFileDTO;
import com.soo.hoo.admin.model.entity.SysBookCover;
import com.soo.hoo.admin.model.vo.BookCoverVO;
import com.soo.hoo.admin.service.BookCoverService;
import com.soo.hoo.admin.service.FileService;
import com.soo.hoo.core.annotation.OperateLog;
import com.soo.hoo.core.enums.BusinessTypeEnum;
import com.soo.hoo.core.model.Result;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 封面管理
 * </p>
 *
 * @author 杨燚
 * @since 2024/4/29
 */
@Tag(name = "封面管理")
@RequestMapping("/cover/")
@RestController
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class CoverController {


    private final BookCoverService coverService;

    private final FileService fileService;

    @PostMapping("upload")
    @Operation(summary = "上传封面图片")
    @OperateLog(title = "上传封面图片", businessType = BusinessTypeEnum.UPLOAD)
    public Result<Void> upload(@RequestParam String coverName, @RequestParam MultipartFile file) {
        ImageFileDTO cover = fileService.uploadImage(file, "cover");
        SysBookCover bookCover = new SysBookCover();
        bookCover.setCoverName(coverName);
        bookCover.setCoverFileId(cover.getId());
        bookCover.setCoverPath(cover.getImagePath());
        coverService.save(bookCover);
        return Result.success();
    }

    @GetMapping("list")
    @Operation(summary = "获取图片列表")
    @OperateLog(title = "获取图片列表", businessType = BusinessTypeEnum.QUERY)
    public Result<List<SysBookCover>> list() {
        List<SysBookCover> list = coverService.list();
        return Result.success(list);
    }
}
